package com.alm.token.widget;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alm.token.utils.EmptyUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;

import cn.addapp.pickers.adapter.ArrayWheelAdapter;
import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.WheelPicker;
import cn.addapp.pickers.util.DateUtils;
import cn.addapp.pickers.util.LogUtils;
import cn.addapp.pickers.widget.WheelListView;
import cn.addapp.pickers.widget.WheelView;

/**
 * Created by en on 2019/10/17.
 */

public class DateTimePicker extends WheelPicker {
    public static final int NONE = -1;
    public static final int YEAR_MONTH_DAY = 0;
    public static final int YEAR_MONTH = 1;
    public static final int MONTH_DAY = 2;
    public static final int HOUR_24 = 3;
    public static final int HOUR_12 = 4;
    private ArrayList<String> years;
    private ArrayList<String> months;
    private ArrayList<String> days;
    private ArrayList<String> hours;
    private ArrayList<String> minutes;
    private ArrayList<String> seconds;
    private String yearLabel;
    private String monthLabel;
    private String dayLabel;
    private String hourLabel;
    private String minuteLabel;
    private String secondLabel;
    private int selectedYearIndex;
    private int selectedMonthIndex;
    private int selectedDayIndex;
    private int selectedHourIndex;
    private int selectedMinuteIndex;
    private int selectedSecondIndex;
    private String selectedHour;
    private String selectedMinute;
    private String selectedSecond;
    private OnWheelListener onWheelListener;
    private OnDateTimePickListener onDateTimePickListener;
    private int dateMode;
    private int timeMode;
    private int startYear;
    private int startMonth;
    private int startDay;
    private int endYear;
    private int endMonth;
    private int endDay;
    private int startHour;
    private int startMinute;
    private int startSecond;
    private int endHour;
    private int endMinute;
    private int endSecond;
    private int stepMinute;
    private int stepHour;
    private int stepSecond;

    public DateTimePicker(Activity activity, int timeMode) {
        this(activity, 0, timeMode);
    }

    public DateTimePicker(Activity activity, int dateMode, int timeMode) {
        super(activity);
        this.years = new ArrayList();
        this.months = new ArrayList();
        this.days = new ArrayList();
        this.hours = new ArrayList();
        this.minutes = new ArrayList();
        this.seconds = new ArrayList();
        this.yearLabel = "年";
        this.monthLabel = "月";
        this.dayLabel = "日";
        this.hourLabel = "时";
        this.minuteLabel = "分";
        this.secondLabel = "秒";
        this.selectedYearIndex = 0;
        this.selectedMonthIndex = 0;
        this.selectedDayIndex = 0;
        this.selectedHourIndex = 0;
        this.selectedMinuteIndex = 0;
        this.selectedSecondIndex = 0;
        this.selectedHour = "";
        this.selectedMinute = "";
        this.selectedSecond = "";
        this.dateMode = 0;
        this.timeMode = 3;
        this.startYear = 2010;
        this.startMonth = 1;
        this.startDay = 1;
        this.endYear = 2020;
        this.endMonth = 12;
        this.endDay = 31;
        this.startSecond = 0;
        this.endSecond = 59;
        this.stepSecond = 1;
        this.startMinute = 0;
        this.endMinute = 59;
        this.stepMinute = 1;
        this.stepHour = 1;
        if (dateMode == -1 && timeMode == -1) {
            throw new IllegalArgumentException("The modes are NONE at the same time");
        } else {
            if (dateMode == 0 && timeMode != -1) {
                if (this.screenWidthPixels < 720) {
                    this.textSize = 14;
                } else if (this.screenWidthPixels < 480) {
                    this.textSize = 12;
                }
            }

            this.dateMode = dateMode;
            if (timeMode == 4) {
                this.startHour = 1;
                this.endHour = 12;
            } else {
                this.startHour = 0;
                this.endHour = 23;
            }

            this.timeMode = timeMode;
        }
    }

    public void setDateRangeStart(int startYear, int startMonth, int startDay) {
        if (this.dateMode == -1) {
            throw new IllegalArgumentException("Date mode invalid");
        } else {
            this.startYear = startYear;
            this.startMonth = startMonth;
            this.startDay = startDay;
        }
    }

    public void setDateRangeEnd(int endYear, int endMonth, int endDay) {
        if (this.dateMode == -1) {
            throw new IllegalArgumentException("Date mode invalid");
        } else {
            this.endYear = endYear;
            this.endMonth = endMonth;
            this.endDay = endDay;
            this.initYearData();
        }
    }

    public void setDateRangeStart(int startYearOrMonth, int startMonthOrDay) {
        if (this.dateMode == -1) {
            throw new IllegalArgumentException("Date mode invalid");
        } else if (this.dateMode == 0) {
            throw new IllegalArgumentException("Not support year/month/day mode");
        } else {
            if (this.dateMode == 1) {
                this.startYear = startYearOrMonth;
                this.startMonth = startMonthOrDay;
            } else if (this.dateMode == 2) {
                int year = Calendar.getInstance(Locale.CHINA).get(1);
                this.startYear = this.endYear = year;
                this.startMonth = startYearOrMonth;
                this.startDay = startMonthOrDay;
            }

        }
    }

    public void setDateRangeEnd(int endYearOrMonth, int endMonthOrDay) {
        if (this.dateMode == -1) {
            throw new IllegalArgumentException("Date mode invalid");
        } else if (this.dateMode == 0) {
            throw new IllegalArgumentException("Not support year/month/day mode");
        } else {
            if (this.dateMode == 1) {
                this.endYear = endYearOrMonth;
                this.endMonth = endMonthOrDay;
            } else if (this.dateMode == 2) {
                this.endMonth = endYearOrMonth;
                this.endDay = endMonthOrDay;
            }

            this.initYearData();
        }
    }

    public void setTimeRangeStart(int startHour, int startMinute, int startSecond) {
        if (this.timeMode == -1) {
            throw new IllegalArgumentException("Time mode invalid");
        } else {
            boolean illegal = false;
            if (startHour < 0 || startMinute < 0 || startSecond < 0 || startMinute > 59 || startSecond > 59) {
                illegal = true;
            }

            if (this.timeMode == 4 && (startHour == 0 || startHour > 12)) {
                illegal = true;
            }

            if (this.timeMode == 3 && startHour >= 24) {
                illegal = true;
            }

            if (illegal) {
                throw new IllegalArgumentException("Time out of range");
            } else {
                this.startHour = startHour;
                this.startMinute = startMinute;
                this.startSecond = startSecond;
            }
        }
    }

    public void setTimeRangeEnd(int endHour, int endMinute, int endSecond) {
        if (this.timeMode == -1) {
            throw new IllegalArgumentException("Time mode invalid");
        } else {
            boolean illegal = false;
            if (endHour < 0 || endMinute < 0 || endSecond < 0 || endMinute > 59 || endSecond > 59) {
                illegal = true;
            }

            if (this.timeMode == 4 && (endHour == 0 || endHour > 12)) {
                illegal = true;
            }

            if (this.timeMode == 3 && endHour >= 24) {
                illegal = true;
            }

            if (illegal) {
                throw new IllegalArgumentException("Time out of range");
            } else {
                this.endHour = endHour;
                this.endMinute = endMinute;
                this.endSecond = endSecond;
                this.initHourData();
            }
        }
    }

    public void setLabel(String yearLabel, String monthLabel, String dayLabel, String hourLabel, String minuteLabel, String secondLabel) {
        this.yearLabel = yearLabel;
        this.monthLabel = monthLabel;
        this.dayLabel = dayLabel;
        this.hourLabel = hourLabel;
        this.minuteLabel = minuteLabel;
        this.secondLabel = secondLabel;
    }

    public void setSelectedItem(int year, int month, int day, int hour, int minute, int second) {
        if (this.dateMode != 0) {
            throw new IllegalArgumentException("Date mode invalid");
        } else {
            LogUtils.verbose(this, "change months and days while set selected");
            this.changeMonthData(year);
            this.changeDayData(year, month);
            this.selectedYearIndex = this.findItemIndex(this.years, year);
            this.selectedMonthIndex = this.findItemIndex(this.months, month);
            this.selectedDayIndex = this.findItemIndex(this.days, day);
            if (this.timeMode != -1) {
                this.selectedHour = DateUtils.fillZero(hour);
                this.selectedMinute = DateUtils.fillZero(minute);
                this.selectedSecond = DateUtils.fillZero(second);
                if (this.hours.size() == 0) {
                    LogUtils.verbose(this, "init hours before make view");
                    this.initHourData();
                }

                this.selectedHourIndex = this.findItemIndex(this.hours, hour);
                this.changeMinuteData(hour);
                this.changeSecondData(hour);
                this.selectedMinuteIndex = this.findItemIndex(this.minutes, minute);
                this.selectedSecondIndex = this.findItemIndex(this.seconds, second);
            }

        }
    }

    public void setSelectedItem(int yearOrMonth, int monthOrDay, int hour, int minute, int second) {
        if (this.dateMode == 0) {
            throw new IllegalArgumentException("Date mode invalid");
        } else {
            if (this.dateMode == 2) {
                LogUtils.verbose(this, "change months and days while set selected");
                int year = Calendar.getInstance(Locale.CHINA).get(1);
                this.startYear = this.endYear = year;
                this.changeMonthData(year);
                this.changeDayData(year, yearOrMonth);
                this.selectedMonthIndex = this.findItemIndex(this.months, yearOrMonth);
                this.selectedDayIndex = this.findItemIndex(this.days, monthOrDay);
            } else if (this.dateMode == 1) {
                LogUtils.verbose(this, "change months while set selected");
                this.changeMonthData(yearOrMonth);
                this.selectedYearIndex = this.findItemIndex(this.years, yearOrMonth);
                this.selectedMonthIndex = this.findItemIndex(this.months, monthOrDay);
            }

            if (this.timeMode != -1) {
                this.selectedHour = DateUtils.fillZero(hour);
                this.selectedMinute = DateUtils.fillZero(minute);
                this.selectedSecond = DateUtils.fillZero(second);
            }

        }
    }

    public void setOnWheelListener(OnWheelListener onWheelListener) {
        this.onWheelListener = onWheelListener;
    }

    public void setOnDateTimePickListener(OnDateTimePickListener listener) {
        this.onDateTimePickListener = listener;
    }

    public String getSelectedYear() {
        if (this.dateMode != 0 && this.dateMode != 1) {
            return "";
        } else {
            if (this.years.size() <= this.selectedYearIndex) {
                this.selectedYearIndex = this.years.size() - 1;
            }

            return (String) this.years.get(this.selectedYearIndex);
        }
    }

    public String getSelectedMonth() {
        if (this.dateMode != -1) {
            if (this.months.size() <= this.selectedMonthIndex) {
                this.selectedMonthIndex = this.months.size() - 1;
            }

            return (String) this.months.get(this.selectedMonthIndex);
        } else {
            return "";
        }
    }

    public String getSelectedDay() {
        if (this.dateMode != 0 && this.dateMode != 2) {
            return "";
        } else {
            if (this.days.size() <= this.selectedDayIndex) {
                this.selectedDayIndex = this.days.size() - 1;
            }

            return (String) this.days.get(this.selectedDayIndex);
        }
    }

    public String getSelectedHour() {
        return this.timeMode != -1 ? this.selectedHour : "";
    }

    public String getSelectedMinute() {
        return this.timeMode != -1 ? this.selectedMinute : "";
    }

    public String getSelectedSecond() {
        return this.timeMode != -1 ? this.selectedSecond : "";
    }

    @NonNull
    @Override
    protected View makeCenterView() {
        if ((this.dateMode == 0 || this.dateMode == 1) && this.years.size() == 0) {
            LogUtils.verbose(this, "init years before make view");
            this.initYearData();
        }

        int selectedYear;
        if (this.dateMode != -1 && this.months.size() == 0) {
            LogUtils.verbose(this, "init months before make view");
            selectedYear = DateUtils.trimZero(this.getSelectedYear());
            this.changeMonthData(selectedYear);
        }

        if ((this.dateMode == 0 || this.dateMode == 2) && this.days.size() == 0) {
            LogUtils.verbose(this, "init days before make view");
            if (this.dateMode == 0) {
                selectedYear = DateUtils.trimZero(this.getSelectedYear());
            } else {
                selectedYear = Calendar.getInstance(Locale.CHINA).get(1);
            }

            int selectedMonth = DateUtils.trimZero(this.getSelectedMonth());
            this.changeDayData(selectedYear, selectedMonth);
        }

        if (this.timeMode != -1 && this.hours.size() == 0) {
            LogUtils.verbose(this, "init hours before make view");
            this.initHourData();
        }

        if (this.timeMode != -1 && this.minutes.size() == 0) {
            LogUtils.verbose(this, "init minutes before make view");
            this.changeMinuteData(DateUtils.trimZero(this.selectedHour));
        }

        if (this.timeMode != -1 && this.seconds.size() == 0) {
            LogUtils.verbose(this, "init seconds before make view");
            this.changeSecondData(DateUtils.trimZero(this.selectedHour));
        }

        LinearLayout layout = new LinearLayout(this.activity);
        layout.setOrientation(0);
        layout.setGravity(17);
        if (this.dateMode != -1 && this.timeMode != -1) {
            layout.setWeightSum(6.0F);
        } else if (this.dateMode == 0) {
            layout.setWeightSum(3.0F);
        } else {
            layout.setWeightSum(3.0F);
        }

        LinearLayout.LayoutParams wheelViewParams;
        if (this.weightEnable) {
            wheelViewParams = new LinearLayout.LayoutParams(0, -2);
            wheelViewParams.weight = 1.0F;
        } else {
            wheelViewParams = new LinearLayout.LayoutParams(-2, -2);
        }

        LinearLayout.LayoutParams labelViewParams = new LinearLayout.LayoutParams(-2, -2);
        TextView labelView;
        if (this.wheelModeEnable) {
            WheelView yearView = new WheelView(this.activity);
            final WheelView monthView = new WheelView(this.activity);
            final WheelView dayView = new WheelView(this.activity);
            WheelView hourView = new WheelView(this.activity);
            final WheelView minuteView = new WheelView(this.activity);
            final WheelView secondView = new WheelView(this.activity);
            if (this.dateMode == 0 || this.dateMode == 1) {
                yearView.setCanLoop(this.canLoop);
                yearView.setTextSize((float) this.textSize);
                yearView.setSelectedTextColor(this.textColorFocus);
                yearView.setUnSelectedTextColor(this.textColorNormal);
                yearView.setLineConfig(this.lineConfig);
                yearView.setAdapter(new ArrayWheelAdapter(this.years));
                yearView.setCurrentItem(this.selectedYearIndex);
                yearView.setDividerType(LineConfig.DividerType.FILL);
                yearView.setLayoutParams(wheelViewParams);
                yearView.setOnItemPickListener(new OnItemPickListener<String>() {
                    @Override
                    public void onItemPicked(int index, String item) {
                        selectedYearIndex = index;
                        if (onWheelListener != null) {
                            onWheelListener.onYearWheeled(selectedYearIndex, item);
                        }

                        if (canLinkage) {
                            LogUtils.verbose(this, "change months after year wheeled");
                            selectedMonthIndex = 0;
                            selectedDayIndex = 0;
                            int selectedYear = DateUtils.trimZero(item);
                            changeMonthData(selectedYear);
                            monthView.setAdapter(new ArrayWheelAdapter(months));
                            monthView.setCurrentItem(selectedMonthIndex);
                            changeDayData(selectedYear, DateUtils.trimZero((String) months.get(selectedMonthIndex)));
                            dayView.setAdapter(new ArrayWheelAdapter(days));
                            dayView.setCurrentItem(selectedDayIndex);
                        }
                    }
                });
                layout.addView(yearView);
                if (!TextUtils.isEmpty(this.yearLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setText(this.yearLabel);
                    layout.addView(labelView);
                }
            }

            if (this.dateMode != -1) {
                monthView.setCanLoop(this.canLoop);
                monthView.setTextSize((float) this.textSize);
                monthView.setSelectedTextColor(this.textColorFocus);
                monthView.setUnSelectedTextColor(this.textColorNormal);
                monthView.setAdapter(new ArrayWheelAdapter(this.months));
                monthView.setLineConfig(this.lineConfig);
                monthView.setCurrentItem(this.selectedMonthIndex);
                monthView.setDividerType(LineConfig.DividerType.FILL);
                monthView.setLayoutParams(wheelViewParams);
                monthView.setOnItemPickListener(new OnItemPickListener<String>() {
                    public void onItemPicked(int index, String item) {
                        selectedMonthIndex = index;
                        if (onWheelListener != null) {
                            onWheelListener.onMonthWheeled(selectedMonthIndex, item);
                        }

                        if (dateMode == 0 || dateMode == 2) {
                            LogUtils.verbose(this, "change days after month wheeled");
                            selectedDayIndex = 0;
                            int selectedYear;
                            if (dateMode == 0) {
                                selectedYear = DateUtils.trimZero(getSelectedYear());
                            } else {
                                selectedYear = Calendar.getInstance(Locale.CHINA).get(1);
                            }

                            changeDayData(selectedYear, DateUtils.trimZero(item));
                            dayView.setAdapter(new ArrayWheelAdapter(days));
                            dayView.setCurrentItem(selectedDayIndex);
                        }

                    }
                });
                layout.addView(monthView);
                if (!TextUtils.isEmpty(this.monthLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setText(this.monthLabel);
                    layout.addView(labelView);
                }
            }

            if (this.dateMode == 0 || this.dateMode == 2) {
                dayView.setCanLoop(this.canLoop);
                dayView.setTextSize((float) this.textSize);
                dayView.setSelectedTextColor(this.textColorFocus);
                dayView.setUnSelectedTextColor(this.textColorNormal);
                dayView.setAdapter(new ArrayWheelAdapter(this.days));
                dayView.setCurrentItem(this.selectedDayIndex);
                dayView.setLineConfig(this.lineConfig);
                dayView.setDividerType(LineConfig.DividerType.FILL);
                dayView.setLayoutParams(wheelViewParams);
                dayView.setOnItemPickListener(new OnItemPickListener<String>() {
                    public void onItemPicked(int index, String item) {
                        selectedDayIndex = index;
                        if (onWheelListener != null) {
                            onWheelListener.onDayWheeled(index, item);
                        }

                    }
                });
                layout.addView(dayView);
                if (!TextUtils.isEmpty(this.dayLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setText(this.dayLabel);
                    layout.addView(labelView);
                }
            }

            if (this.timeMode != -1) {
                hourView.setCanLoop(this.canLoop);
                hourView.setTextSize((float) this.textSize);
                hourView.setSelectedTextColor(this.textColorFocus);
                hourView.setUnSelectedTextColor(this.textColorNormal);
                hourView.setDividerType(LineConfig.DividerType.FILL);
                hourView.setAdapter(new ArrayWheelAdapter(this.hours));
                hourView.setCurrentItem(this.selectedHourIndex);
                hourView.setLineConfig(this.lineConfig);
                hourView.setLayoutParams(wheelViewParams);
                hourView.setOnItemPickListener(new OnItemPickListener<String>() {
                    public void onItemPicked(int index, String item) {
                        selectedHourIndex = index;
                        selectedMinuteIndex = 0;
                        selectedSecondIndex = 0;
                        selectedHour = item;
                        if (onWheelListener != null) {
                            onWheelListener.onHourWheeled(index, item);
                        }

                        if (canLinkage) {
                            changeMinuteData(DateUtils.trimZero(item));
                            changeSecondData(DateUtils.trimZero(item));
                            minuteView.setAdapter(new ArrayWheelAdapter(minutes));
                            minuteView.setCurrentItem(selectedMinuteIndex);
                            secondView.setAdapter(new ArrayWheelAdapter(seconds));
                            secondView.setCurrentItem(selectedSecondIndex);
                        }
                    }
                });
                layout.addView(hourView);
                if (!TextUtils.isEmpty(this.hourLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setText(this.hourLabel);
                    layout.addView(labelView);
                }

                minuteView.setCanLoop(this.canLoop);
                minuteView.setTextSize((float) this.textSize);
                minuteView.setSelectedTextColor(this.textColorFocus);
                minuteView.setUnSelectedTextColor(this.textColorNormal);
                minuteView.setAdapter(new ArrayWheelAdapter(this.minutes));
                minuteView.setCurrentItem(this.selectedMinuteIndex);
                minuteView.setDividerType(LineConfig.DividerType.FILL);
                minuteView.setLineConfig(this.lineConfig);
                minuteView.setLayoutParams(wheelViewParams);
                layout.addView(minuteView);
                minuteView.setOnItemPickListener(new OnItemPickListener<String>() {
                    public void onItemPicked(int index, String item) {
                        selectedMinuteIndex = index;
                        selectedMinute = item;
                        if (onWheelListener != null) {
                            onWheelListener.onMinuteWheeled(index, item);
                        }

                    }
                });
                if (!TextUtils.isEmpty(this.minuteLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setText(this.minuteLabel);
                    layout.addView(labelView);
                }

                secondView.setCanLoop(this.canLoop);
                secondView.setTextSize((float) this.textSize);
                secondView.setSelectedTextColor(this.textColorFocus);
                secondView.setUnSelectedTextColor(this.textColorNormal);
                secondView.setAdapter(new ArrayWheelAdapter(this.seconds));
                secondView.setCurrentItem(this.selectedSecondIndex);
                secondView.setDividerType(LineConfig.DividerType.FILL);
                secondView.setLineConfig(this.lineConfig);
                secondView.setLayoutParams(wheelViewParams);
                layout.addView(secondView);
                secondView.setOnItemPickListener(new OnItemPickListener<String>() {
                    @Override
                    public void onItemPicked(int index, String item) {
                        selectedSecondIndex = index;
                        selectedSecond = item;
                        if (onWheelListener != null) {
                            onWheelListener.onSecondWheeled(index, item);
                        }

                    }
                });
                if (!TextUtils.isEmpty(this.secondLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setText(this.secondLabel);
                    layout.addView(labelView);
                }
            }
        } else {
            WheelListView yearView = new WheelListView(this.activity);
            final WheelListView monthView = new WheelListView(this.activity);
            final WheelListView dayView = new WheelListView(this.activity);
            WheelListView hourView = new WheelListView(this.activity);
            final WheelListView minuteView = new WheelListView(this.activity);
            final WheelListView secondView = new WheelListView(this.activity);
            if (this.dateMode == 0 || this.dateMode == 1) {
                yearView.setLayoutParams(wheelViewParams);
                yearView.setTextSize(this.textSize);
                yearView.setSelectedTextColor(this.textColorFocus);
                yearView.setUnSelectedTextColor(this.textColorNormal);
                yearView.setLineConfig(this.lineConfig);
                yearView.setOffset(this.offset);
                yearView.setCanLoop(this.canLoop);
                yearView.setItems(this.years, this.selectedYearIndex);
                yearView.setOnWheelChangeListener(new WheelListView.OnWheelChangeListener() {
                    @Override
                    public void onItemSelected(int index, String item) {
                        selectedYearIndex = index;
                        if (onWheelListener != null) {
                            onWheelListener.onYearWheeled(selectedYearIndex, item);
                        }

                        if (canLinkage) {
                            selectedMonthIndex = 0;
                            selectedDayIndex = 0;
                            int selectedYear = DateUtils.trimZero(item);
                            changeMonthData(selectedYear);
                            monthView.setItems(months, selectedMonthIndex);
                            changeDayData(selectedYear, DateUtils.trimZero((String) months.get(selectedMonthIndex)));
                            dayView.setItems(days, selectedDayIndex);
                        }
                    }
                });
                layout.addView(yearView);
                if (!TextUtils.isEmpty(this.yearLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setText(this.yearLabel);
                    layout.addView(labelView);
                }
            }

            if (this.dateMode != -1) {
                monthView.setLayoutParams(wheelViewParams);
                monthView.setTextSize(this.textSize);
                monthView.setSelectedTextColor(this.textColorFocus);
                monthView.setUnSelectedTextColor(this.textColorNormal);
                monthView.setLineConfig(this.lineConfig);
                monthView.setOffset(this.offset);
                monthView.setCanLoop(this.canLoop);
                monthView.setItems(this.months, this.selectedMonthIndex);
                monthView.setOnWheelChangeListener(new WheelListView.OnWheelChangeListener() {
                    @Override
                    public void onItemSelected(int index, String item) {
                        selectedMonthIndex = index;
                        if (onWheelListener != null) {
                            onWheelListener.onMonthWheeled(selectedMonthIndex, item);
                        }

                        if (dateMode == 0 || dateMode == 2) {
                            LogUtils.verbose(this, "change days after month wheeled");
                            selectedDayIndex = 0;
                            int selectedYear;
                            if (dateMode == 0) {
                                selectedYear = DateUtils.trimZero(getSelectedYear());
                            } else {
                                selectedYear = Calendar.getInstance(Locale.CHINA).get(1);
                            }

                            changeDayData(selectedYear, DateUtils.trimZero(item));
                            dayView.setItems(days, selectedDayIndex);
                        }

                    }
                });
                layout.addView(monthView);
                if (!TextUtils.isEmpty(this.monthLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setText(this.monthLabel);
                    layout.addView(labelView);
                }
            }

            if (this.dateMode == 0 || this.dateMode == 2) {
                dayView.setLayoutParams(wheelViewParams);
                dayView.setTextSize(this.textSize);
                dayView.setSelectedTextColor(this.textColorFocus);
                dayView.setUnSelectedTextColor(this.textColorNormal);
                dayView.setLineConfig(this.lineConfig);
                dayView.setOffset(this.offset);
                dayView.setCanLoop(this.canLoop);
                dayView.setItems(this.days, this.selectedDayIndex);
                dayView.setOnWheelChangeListener(new WheelListView.OnWheelChangeListener() {
                    @Override
                    public void onItemSelected(int index, String item) {
                        selectedDayIndex = index;
                        if (onWheelListener != null) {
                            onWheelListener.onDayWheeled(selectedDayIndex, item);
                        }

                    }
                });
                layout.addView(dayView);
                if (!TextUtils.isEmpty(this.dayLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setText(this.dayLabel);
                    layout.addView(labelView);
                }
            }

            if (this.timeMode != -1) {
                hourView.setLayoutParams(wheelViewParams);
                hourView.setTextSize(this.textSize);
                hourView.setSelectedTextColor(this.textColorFocus);
                hourView.setUnSelectedTextColor(this.textColorNormal);
                hourView.setLineConfig(this.lineConfig);
                hourView.setCanLoop(this.canLoop);
                hourView.setItems(this.hours, this.selectedHour);
                hourView.setOnWheelChangeListener(new WheelListView.OnWheelChangeListener() {
                    @Override
                    public void onItemSelected(int index, String item) {
                        selectedHourIndex = index;
                        selectedMinuteIndex = 0;
                        selectedSecondIndex = 0;
                        selectedHour = item;
                        if (onWheelListener != null) {
                            onWheelListener.onHourWheeled(index, item);
                        }

                        if (canLinkage) {
                            changeMinuteData(DateUtils.trimZero(item));
                            minuteView.setItems(minutes, selectedMinuteIndex);
                            secondView.setItems(seconds, selectedSecondIndex);
                        }
                    }
                });
                layout.addView(hourView);
                if (!TextUtils.isEmpty(this.hourLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setText(this.hourLabel);
                    layout.addView(labelView);
                }

                minuteView.setLayoutParams(wheelViewParams);
                minuteView.setTextSize(this.textSize);
                minuteView.setSelectedTextColor(this.textColorFocus);
                minuteView.setUnSelectedTextColor(this.textColorNormal);
                minuteView.setLineConfig(this.lineConfig);
                minuteView.setOffset(this.offset);
                minuteView.setCanLoop(this.canLoop);
                minuteView.setItems(this.minutes, this.selectedMinute);
                minuteView.setOnWheelChangeListener(new WheelListView.OnWheelChangeListener() {
                    @Override
                    public void onItemSelected(int index, String item) {
                        selectedMinuteIndex = index;
                        selectedMinute = item;
                        if (onWheelListener != null) {
                            onWheelListener.onMinuteWheeled(index, item);
                        }

                    }
                });
                layout.addView(minuteView);
                if (!TextUtils.isEmpty(this.minuteLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setText(this.minuteLabel);
                    layout.addView(labelView);
                }

                secondView.setLayoutParams(wheelViewParams);
                secondView.setTextSize(this.textSize);
                secondView.setSelectedTextColor(this.textColorFocus);
                secondView.setUnSelectedTextColor(this.textColorNormal);
                secondView.setLineConfig(this.lineConfig);
                secondView.setOffset(this.offset);
                secondView.setCanLoop(this.canLoop);
                secondView.setItems(this.seconds, this.selectedSecond);
                secondView.setOnWheelChangeListener(new WheelListView.OnWheelChangeListener() {
                    @Override
                    public void onItemSelected(int index, String item) {
                        selectedSecondIndex  = index;
                        selectedSecond = item;
                        if (onWheelListener != null) {
                            onWheelListener.onSecondWheeled(index, item);
                        }

                    }
                });
                layout.addView(secondView);
                if (!TextUtils.isEmpty(this.secondLabel)) {
                    labelView = new TextView(this.activity);
                    labelView.setLayoutParams(labelViewParams);
                    labelView.setTextSize((float) this.textSize);
                    labelView.setTextColor(this.textColorFocus);
                    labelView.setText(this.secondLabel);
                    layout.addView(labelView);
                }
            }
        }

        return layout;
    }

    @Override
    protected void onSubmit() {
        if (this.onDateTimePickListener != null) {
            String year = this.getSelectedYear();
            String month = this.getSelectedMonth();
            String day = this.getSelectedDay();
            String hour = this.getSelectedHour();
            String minute = this.getSelectedMinute();
            String second = this.getSelectedSecond();

            switch (this.dateMode) {
                case -1:
                    ((OnTimePickListener) this.onDateTimePickListener).onDateTimePicked(hour, minute);
                    break;
                case 0:
                    ((OnYearMonthDayTimePickListener) this.onDateTimePickListener).onDateTimePicked(year, month, day, hour, minute,second);
                    break;
                case 1:
                    ((OnYearMonthTimePickListener) this.onDateTimePickListener).onDateTimePicked(year, month, hour, minute);
                    break;
                case 2:
                    ((OnMonthDayTimePickListener) this.onDateTimePickListener).onDateTimePicked(month, day, hour, minute);
            }

        }
    }

    private int findItemIndex(ArrayList<String> items, int item) {
        int index = Collections.binarySearch(items, Integer.valueOf(item), new Comparator<Object>() {
            public int compare(Object lhs, Object rhs) {
                String lhsStr = lhs.toString();
                String rhsStr = rhs.toString();
                lhsStr = lhsStr.startsWith("0") ? lhsStr.substring(1) : lhsStr;
                rhsStr = rhsStr.startsWith("0") ? rhsStr.substring(1) : rhsStr;
                lhsStr=EmptyUtils.isNotEmpty(lhsStr)?lhsStr:"0";
                rhsStr=EmptyUtils.isNotEmpty(rhsStr)?rhsStr:"0";
                return Integer.parseInt(lhsStr) - Integer.parseInt(rhsStr);
            }
        });
        if (index < 0) {
            throw new IllegalArgumentException("Item[" + item + "] out of range");
        } else {
            return index;
        }
    }

    private void initYearData() {
        this.years.clear();
        if (this.startYear == this.endYear) {
            this.years.add(String.valueOf(this.startYear));
        } else {
            int i;
            if (this.startYear < this.endYear) {
                for (i = this.startYear; i <= this.endYear; ++i) {
                    this.years.add(String.valueOf(i));
                }
            } else {
                for (i = this.startYear; i >= this.endYear; --i) {
                    this.years.add(String.valueOf(i));
                }
            }
        }

    }

    private void changeMonthData(int selectedYear) {
        this.months.clear();
        if (this.startMonth >= 1 && this.endMonth >= 1 && this.startMonth <= 12 && this.endMonth <= 12) {
            int i;
            if (this.startYear == this.endYear) {
                if (this.startMonth > this.endMonth) {
                    for (i = this.endMonth; i >= this.startMonth; --i) {
                        this.months.add(DateUtils.fillZero(i));
                    }
                } else {
                    for (i = this.startMonth; i <= this.endMonth; ++i) {
                        this.months.add(DateUtils.fillZero(i));
                    }
                }
            } else if (selectedYear == this.startYear) {
                for (i = this.startMonth; i <= 12; ++i) {
                    this.months.add(DateUtils.fillZero(i));
                }
            } else if (selectedYear == this.endYear) {
                for (i = 1; i <= this.endMonth; ++i) {
                    this.months.add(DateUtils.fillZero(i));
                }
            } else {
                for (i = 1; i <= 12; ++i) {
                    this.months.add(DateUtils.fillZero(i));
                }
            }

        } else {
            throw new IllegalArgumentException("Month out of range [1-12]");
        }
    }

    private void changeDayData(int selectedYear, int selectedMonth) {
        int maxDays = DateUtils.calculateDaysInMonth(selectedYear, selectedMonth);
        this.days.clear();
        int i;
        if (selectedYear == this.startYear && selectedMonth == this.startMonth && selectedYear == this.endYear && selectedMonth == this.endMonth) {
            for (i = this.startDay; i <= this.endDay; ++i) {
                this.days.add(DateUtils.fillZero(i));
            }
        } else if (selectedYear == this.startYear && selectedMonth == this.startMonth) {
            for (i = this.startDay; i <= maxDays; ++i) {
                this.days.add(DateUtils.fillZero(i));
            }
        } else if (selectedYear == this.endYear && selectedMonth == this.endMonth) {
            for (i = 1; i <= this.endDay; ++i) {
                this.days.add(DateUtils.fillZero(i));
            }
        } else {
            for (i = 1; i <= maxDays; ++i) {
                this.days.add(DateUtils.fillZero(i));
            }
        }

    }

    private void initHourData() {
        for (int i = this.startHour; i <= this.endHour; i += this.stepHour) {
            String hour = DateUtils.fillZero(i);
            this.hours.add(hour);
        }

        if (this.hours.indexOf(this.selectedHour) == -1) {
            this.selectedHour = (String) this.hours.get(0);
        }

    }

    private void changeMinuteData(int selectedHour) {
        int i;
        if (this.startHour == this.endHour) {
            if (this.startMinute > this.endMinute) {
                i = this.startMinute;
                this.startMinute = this.endMinute;
                this.endMinute = i;
            }

            for (i = this.startMinute; i <= this.endMinute; i += this.stepMinute) {
                this.minutes.add(DateUtils.fillZero(i));
            }
        } else if (selectedHour == this.startHour) {
            for (i = this.startMinute; i <= 59; i += this.stepMinute) {
                this.minutes.add(DateUtils.fillZero(i));
            }
        } else if (selectedHour == this.endHour) {
            for (i = 0; i <= this.endMinute; i += this.stepMinute) {
                this.minutes.add(DateUtils.fillZero(i));
            }
        } else {
            for (i = 0; i <= 59; i += this.stepMinute) {
                this.minutes.add(DateUtils.fillZero(i));
            }
        }

        if (this.minutes.indexOf(this.selectedMinute) == -1) {
            this.selectedMinute = (String) this.minutes.get(0);
        }

    }


    private void changeSecondData(int selectedHour) {
        int i;
        if (this.startHour == this.endHour) {
            if (this.startSecond > this.endSecond) {
                i = this.startSecond;
                this.startSecond = this.endSecond;
                this.endSecond = i;
            }

            for (i = this.startSecond; i <= this.endSecond; i += this.stepSecond) {
                this.seconds.add(DateUtils.fillZero(i));
            }
        } else if (selectedHour == this.startHour) {
            for (i = this.startSecond; i <= 59; i += this.stepSecond) {
                this.seconds.add(DateUtils.fillZero(i));
            }
        } else if (selectedHour == this.endHour) {
            for (i = 0; i <= this.endSecond; i += this.stepSecond) {
                this.seconds.add(DateUtils.fillZero(i));
            }
        } else {
            for (i = 0; i <= 59; i += this.stepSecond) {
                this.seconds.add(DateUtils.fillZero(i));
            }
        }

        if (this.seconds.indexOf(this.selectedSecond) == -1) {
            this.selectedSecond = (String) this.seconds.get(0);
        }

    }

    public interface OnTimePickListener extends OnDateTimePickListener {
        void onDateTimePicked(String var1, String var2);
    }

    public interface OnMonthDayTimePickListener extends OnDateTimePickListener {
        void onDateTimePicked(String var1, String var2, String var3, String var4);
    }

    public interface OnYearMonthTimePickListener extends OnDateTimePickListener {
        void onDateTimePicked(String var1, String var2, String var3, String var4);
    }

    public interface OnYearMonthDayTimePickListener extends OnDateTimePickListener {
        void onDateTimePicked(String var1, String var2, String var3, String var4, String var5,String var6);
    }

    protected interface OnDateTimePickListener {
    }

    public interface OnWheelListener {
        void onYearWheeled(int var1, String var2);

        void onMonthWheeled(int var1, String var2);

        void onDayWheeled(int var1, String var2);

        void onHourWheeled(int var1, String var2);

        void onMinuteWheeled(int var1, String var2);

        void onSecondWheeled(int var1, String var2);
    }

    @Retention(RetentionPolicy.SOURCE)
    public @interface TimeMode {
    }

    @Retention(RetentionPolicy.SOURCE)
    public @interface DateMode {
    }

}